<template>
	<div>
		<AplidForm ref="formRef" v-bind="BaseFormConfig" v-model="formModel" @submit="onSubmit"> </AplidForm>
	</div>
</template>

<script setup lang="ts">
import apiMod from '@/apiMod'
import { api, BaseFormConfig } from '../../config'
import { getAreaObj } from '@/utils/util'
import { useCommonStore } from '@/stores/common'
const { setTempParams ,setLoading} = useCommonStore()
import { useOperate } from '@/views/datacenter/hooks/useConfig'
const { getValue } = useOperate(5)

type Props = {
	nowRowData?: any
}
const props = withDefaults(defineProps<Props>(), {})

const emit = defineEmits(['submit'])
const formModel: any = ref({})
const formRef = ref()
let baseData = {}

const onSubmit = (form, done) => {
	const crIds: any = getAreaObj(form.crAreas, 'cr')
	const crNames: any = getAreaObj(form.crAreasName, 'cr')

	const arIds: any = getAreaObj(form.areas, '')
	const arNames: any = getAreaObj(form.areasName, '')

	const unitAreaIds: any = getAreaObj(form.unitAreas, 'unit')
	const unitAreaNames: any = getAreaObj(form.unitAddress, 'unit')
	const params = {
		...form,
		...crIds,
		...crNames,
		...arIds,
		...arNames,
		...unitAreaIds,
		...unitAreaNames,
		code: 2007,
	}

	const emptyObj = {
		crAreas: undefined,
		crAreasName: undefined,
		areas: undefined,
		areasName: undefined,
		code: undefined,
		unitAreas: undefined,
		unitAreasName: undefined,
		unitAddress: undefined,
	}

	// 变更字段
	let changeColumnsBase = BaseFormConfig.column.filter((item) => item.changeField)
	// 处理变更数据
	let changeData = []
	changeColumnsBase.map((item) => {
		const tempBefore = baseData[item.propName || item.prop]
		const tempAfter = params[item.propName || item.prop]
		if (tempBefore != tempAfter) {
			changeData.push({
				label: item.label,
				before: getValue(tempBefore, item),
				after: getValue(tempAfter, item),
			})
		}
	})

	const changeObj = {
		data: changeData,
		dataChangeContentList: [
			{
				changeBefore: {
					...baseData,
					...emptyObj,
				},
				changeAfter: {
					...params,
					...emptyObj,
				},
				changeData: {
					...params,
					...emptyObj,
				},
				code: 2007,
			},
		],
	}

	setTempParams({
		noChange: params.workType == 2,
		name: params.practitionersName,
		idCard: params.idCard,
		nowChange: changeObj,
	})
	emit('submit', params)
	done()
}

const onSave = () => {
	formRef.value!.onSubmit()
}

const getInitData = () => {
  setLoading()
	apiMod[api.detail](props.nowRowData.id).then((res) => {
		const data = res.data
		const areaInfo: any = getAreaObj(data, '', 'set')
		const crAreasInfo: any = getAreaObj(data, 'cr', 'set')
		const unitAreasInfo: any = getAreaObj(data, 'unit', 'set')
    setLoading()
		formModel.value = {
			...data,
			areas: areaInfo.areaIds,
			areasName: areaInfo.names,

			crAreas: crAreasInfo.areaIds,
			crAreasName: crAreasInfo.names,

			unitAreas: unitAreasInfo.areaIds,
			unitAddress: unitAreasInfo.names,
		}
		baseData = Object.freeze(
			JSON.parse(
				JSON.stringify({
					...data,
					areas: areaInfo.areaIds,
					areasName: areaInfo.names,

					crAreas: crAreasInfo.areaIds,
					crAreasName: crAreasInfo.names,

					unitAreas: unitAreasInfo.areaIds,
					unitAddress: unitAreasInfo.names,
				})
			)
		)
	})
}
onMounted(() => {
	getInitData()
})

defineExpose({
	onSave,
})
</script>

<style scoped></style>
